<template>
	<view class="accountBox">
		<view class="navBox">
			<u-navbar title="老师账户" :autoBack="true" :titleStyle="{fontWeight:'900'}" bgColor="#ffffff">
			</u-navbar>
		</view>
		<view class="addBox" :style="'top:'+(20+(customBarHeight*2))+'rpx;'" v-if="showAdd&&teacherInfo.type==1"
			@click="showAdd=false">
			<image src="../../static/images/add.png" class="icon"></image>
			<view class="label">添加老师账号</view>
		</view>
		<view class="accountInfo" :style="'top:'+(20+(customBarHeight*2))+'rpx;'" v-if="!showAdd&&teacherInfo.type==1">
			<view class="label">老师账户:</view>
			<view class="input">
				<u-input border="none" placeholder="请输入注册的老师账户" v-model="account"></u-input>
			</view>
			<view class="confirm" @click="bindTeacher">确认</view>
		</view>
		<view class="teacherInfoBox" :style="'top:'+(20+(customBarHeight*2))+'rpx;'" v-if="teacherInfo.type==2">
			<image src="../../static/images/accountBg.png" class="accountBg"></image>
			<view class="avatarBox">
				<image :src="teacherInfo.avatar" class="avatar"></image>
				<view class="tagBox">
					<image src="../../static/images/tag.png" class="tag"></image>
					<view class="label">老师账户</view>
				</view>
			</view>
			<view class="teacherInfo">
				<view class="nickName">{{teacherInfo.nickname}}</view>
				<view class="phone">{{teacherInfo.mobile}}</view>
			</view>
			<view class="unbindBox" @click="showPopup = true">
				<image src="../../static/images/unbind.png" class="unbind"></image>
				<view class="label">账户解绑</view>
			</view>
		</view>

		<u-popup :show="showPopup" mode="center" :round="24">
			<view class="popupBox">
				<view class="tipsBox">解除当前老师账户后，将恢复到普通用户模式，部分功能无法使用，且无法恢复现有数据，是否确认解绑!</view>
				<view class="buttonGroup">
					<view class="cancel" @click="showPopup = false">取消</view>
					<view class="confirm" @click="unbindTeacher">确认</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getUserInfo,
		bindTeacherAccount,
		unbindTeacherAccount,
	} from "@/api/api.js"
	const app = getApp();
	export default {
		data() {
			return {
				showAdd: true,
				teacherInfo: null,
				account: '',
				showPopup: false,
				customBarHeight: Number(44 + app.globalData.statusBar),
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			//获取用户信息
			getUserInfo() {
				getUserInfo().then(res => {
					this.$store.commit('UPDATE_USERINFO', res.data);
					this.teacherInfo = res.data
				})
			},
			//绑定老师账户
			bindTeacher() {
				bindTeacherAccount({
					mobile: this.account
				}).then(res => {
					this.getUserInfo()
				})
			},
			//解除绑定老师账户
			unbindTeacher() {
				unbindTeacherAccount().then(res => {
					this.showPopup = false
					this.teacherInfo = null
					this.showAdd = true
					this.account = null
					this.getUserInfo()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.accountBox {
		min-height: 100vh;
		background-color: #f8f8f8;
	}

	.navBox {
		position: relative;
		z-index: 1000;
	}

	.addBox {
		position: relative;
		height: 88rpx;
		top: 160rpx;
		margin: 0 34rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #049FFF;
		border-radius: 24rpx;

		.icon {
			width: 40rpx;
			height: 40rpx;
		}

		.label {
			margin-left: 24rpx;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}

	.accountInfo {
		position: relative;
		top: 160rpx;
		margin: 0 34rpx;

		.label {
			font-weight: bold;
			font-size: 34rpx;
			color: #000000;
		}

		.input {
			height: 88rpx;
			background: #FFFFFF;
			border-radius: 12rpx;
			margin: 12rpx 0 40rpx;
		}

		/deep/ .input .u-input {
			height: 100%;
			padding: 0 20rpx !important;
		}

		.confirm {
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #049FFF;
			border-radius: 24rpx;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}

	.teacherInfoBox {
		position: relative;
		top: 160rpx;
		margin: 0 34rpx;
		height: 242rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 28rpx;

		.accountBg {
			position: absolute;
			width: 100%;
			height: 242rpx;
		}

		.avatarBox {
			position: relative;

			.avatar {
				width: 140rpx;
				height: 140rpx;
				border-radius: 50%;
				border: 2px solid #ffffff;
				overflow: hidden;
			}

			.tagBox {
				position: absolute;
				bottom: 18rpx;
				left: 0;
				text-align: right;

				.tag {
					position: absolute;
					width: 140rpx;
					height: 36rpx;
				}

				.label {
					font-size: 24rpx;
					position: absolute;
					width: 140rpx;
					line-height: 36rpx;
					color: #FFFFFF;
					left: -4rpx;
				}
			}
		}

		.teacherInfo {
			position: relative;
			flex: 1;
			margin-left: 14rpx;
			color: #FFFFFF;

			.nickName {
				font-size: 36rpx;
				margin-bottom: 12rpx;
			}

			.phone {
				font-size: 28rpx;
			}
		}

		.unbindBox {
			position: relative;
			width: 130rpx;
			height: 96rpx;
			background: #FFFFFF;
			border-radius: 18rpx;
			text-align: center;
			box-sizing: border-box;
			padding: 14rpx;

			.unbind {
				width: 40rpx;
				height: 40rpx;
			}

			.label {
				font-size: 24rpx;
				color: #49A1FD;
			}
		}
	}

	.popupBox {
		position: relative;
		box-sizing: border-box;
		height: 364rpx;
		width: 682rpx;
		background: linear-gradient(180deg, #DFFAFF 0%, #FFFFFF 100%);
		border-radius: 23rpx;
		border: 2rpx solid #FFFFF;
		padding: 62rpx 64rpx 38rpx;

		.tipsBox {
			font-size: 28rpx;
			color: #000000;
		}

		.buttonGroup {
			display: flex;
			justify-content: space-between;
			margin-top: 56rpx;

			.cancel,
			.confirm {
				width: 268rpx;
				height: 88rpx;
				text-align: center;
				line-height: 88rpx;
				font-size: 32rpx;
				color: #FFFFFF
			}

			.cancel {

				background: #E5E5E5;
				border-radius: 16rpx;
			}

			.confirm {
				background: #00A6FF;
				border-radius: 16rpx;
			}
		}
	}
</style>